#include "gateway_server.hpp"
#include <gflags/gflags.h>
DEFINE_bool(run_mode, false, "程序的运行模式, false-调试; true-发布;");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "日志等级: 0-TRACE, 1-DEBUG, 2-INFO, 3-WARN, 4-ERROR, 5-FATAL");

DEFINE_int32(http_port, 9000, "http服务器端口号");
DEFINE_int32(websocket_port, 9001, "websocket服务器端口号");

DEFINE_string(redis_host, "127.0.0.1", "Redis服务器访问地址");
DEFINE_int32(redis_port, 6379, "Redis服务器访问端口");
DEFINE_int32(redis_db, 0, "Redis默认库号");
DEFINE_bool(redis_keep_alive, true, "Redis长连接保活选项");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "etcd的地址");
DEFINE_string(base_service, "/service", "服务注册的根目录");
DEFINE_string(message_instance_name, "/message_service", "消息存储服务名称");
DEFINE_string(user_instance_name, "/user_service", "用户管理服务名称");
DEFINE_string(file_instance_name, "/file_service", "文件服务名称");
DEFINE_string(speech_instance_name, "/speech_service", "语音服务名称");
DEFINE_string(friend_instance_name, "/friend_service", "好友服务名称");
DEFINE_string(transmite_instance_name, "/transmite_service", "转发服务名称");


int main(int argn, char* argv[]) {
    google::ParseCommandLineFlags(&argn, &argv, true);
    init_logger(FLAGS_log_file, int_log_level_map(FLAGS_log_level), FLAGS_run_mode);
    GatewayServerBuilder builder;
    builder.make_discovery_object(
        FLAGS_etcd_host, 
        FLAGS_base_service, 
        FLAGS_file_instance_name,
        FLAGS_speech_instance_name,
        FLAGS_message_instance_name,
        FLAGS_friend_instance_name,
        FLAGS_user_instance_name,
        FLAGS_transmite_instance_name
    );
    builder.make_redis_object(FLAGS_redis_host, FLAGS_redis_port, FLAGS_redis_db, FLAGS_redis_keep_alive);
    builder.make_server_object(FLAGS_websocket_port, FLAGS_http_port);
    GatewayServer::ptr gateway_server = builder.build();
    if(!gateway_server)
    {
        LOG_ERROR("创建网关失败");
        return -1;
    }
    gateway_server->start();

    return 0;
}